﻿using CommonType;
using MediatR;
using S_Examine.API.Application.Commands.AssessmentIndexCommand;
using S_Examine.Infrastructure.Interface;

namespace S_Examine.API.Application.CommandHandlers.AssessmentIndexHandler
{
	public class AssessmentIndexDeleteCommandHandler : IRequestHandler<AssessmentIndexDeleteCommand, ApiResule<int>>
	{
		private readonly IAssessmentIndexRepository _assessmentIndexRepository;

		public AssessmentIndexDeleteCommandHandler(IAssessmentIndexRepository assessmentIndexRepository)
		{
			_assessmentIndexRepository = assessmentIndexRepository;
		}

		public async Task<ApiResule<int>> Handle(AssessmentIndexDeleteCommand request, CancellationToken cancellationToken)
		{
			var assessment = await _assessmentIndexRepository.GetByIdAsync(request.Id);

			if (assessment != null) assessment.IsDel = true;

			var result = await _assessmentIndexRepository.UpdateAsync(assessment);

			if (result > 0)
			{
				return new ApiResule<int>
				{
					Code = 200,
					Msg = "成功",
					Result = result
				};
			}

			return new ApiResule<int>
			{
				Code = 201,
				Msg = "失败",
				Result = result
			};
		}
	}
}
